package com.bw.produce.service.impl;

import com.bw.common.core.domain.R;
import com.bw.common.core.exception.produce.RepeatException;
import com.bw.produce.api.RemoteFileService;
import com.bw.produce.domain.Classify;
import com.bw.produce.domain.ClassifyTypeMid;
import com.bw.produce.domain.dto.ClassifyDTO;
import com.bw.produce.domain.vo.ClassifySelectVO;
import com.bw.produce.domain.vo.ClassifyVO;
import com.bw.produce.mapper.ClassifyMapper;
import com.bw.produce.query.ClassifyQueryObject;
import com.bw.produce.service.IClassifyService;
import com.bw.system.api.domain.SysFile;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @author lemon
 * @date 2024/11/14 22:25
 **/
@Service
public class ClassifyServiceImpl implements IClassifyService {

    @Autowired
    ClassifyMapper classifyMapper;


    @Autowired
    RemoteFileService remoteFileService;

    @Override
    public List<ClassifyVO> selectClassifyList(ClassifyQueryObject queryParams) {
        List<ClassifyVO> list;
        if (queryParams.getParentId()==0){
            list = classifyMapper.selectFirst(queryParams);
        }else {
            list = classifyMapper.selectSecond(queryParams);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertClassify(ClassifyDTO classifyDTO) {

        Classify classify = new Classify();
        BeanUtils.copyProperties(classifyDTO,classify);
        classify.setLevel(classify.getParentId() == 0?1:2);
        classifyMapper.insert(classify);

        //保存属性
        List<Integer> types = classifyDTO.getTypeWithProperty();
        Long id = classify.getId();
        saveRelation(types,id);
    }

    protected void saveRelation(List<Integer> types,Long classifyId){
        ClassifyTypeMid mid = new ClassifyTypeMid();
        mid.setClassifyId(classifyId);
        mid.setTypeId(types.get(0));
        mid.setPropertyId(types.get(1));
        classifyMapper.saveRelation(mid);
    }

    @Override
    public void updateClassify(ClassifyDTO classifyDTO) {
        Classify classify = new Classify();
        BeanUtils.copyProperties(classifyDTO,classify);
        classifyMapper.update(classify);

        //删除属性
        Long id = classify.getId();
        classifyMapper.deleteRelation(id);

        //保存属性
        List<Integer> typeWithProperty = classifyDTO.getTypeWithProperty();
        saveRelation(typeWithProperty,id);
    }


    @Override
    public void deleteClassifyById(Long[] classifyId) {
        for (Long id : classifyId) {
            int count = classifyMapper.selectChildren(id);
            if (count != 0){
                throw new RepeatException("存在子级分类不能删除");
            }
            //删除中间表关系
            classifyMapper.deleteRelation(id);
        }
        //删除分类
        classifyMapper.deleteByIds(classifyId);
    }

    @Override
    public ClassifyVO getInfo(Long classifyId) {
        return classifyMapper.selectInfo(classifyId);
    }

    @Override
    public void changeNavigation(Long classifyId) {
        classifyMapper.updateNavigation(classifyId);
    }

    @Override
    public void changeReveal(Long classifyId) {
        classifyMapper.updateReveal(classifyId);
    }

    @Override
    public List<ClassifySelectVO> getFirstClassify() {
        return classifyMapper.selectFirstClassify();
    }


}
